import {
  _decorator,
  Component,
  Node,
  Collider,
  EPhysics2DDrawFlags,
  RigidBody,
  Constraint,
  PhysicsSystem2D,
  BoxCollider,
  director,
  Contact2DType,
  Collider2D,
  IPhysics2DContact,
  BoxCollider2D,
  PolygonCollider2D,
} from 'cc'
const { ccclass, property } = _decorator

@ccclass('WallCtrl')
export class WallCtrl extends Component {
  onLoad() {
    //绘制物理信息
    PhysicsSystem2D.instance.enable = true
    PhysicsSystem2D.instance.debugDrawFlags =
      EPhysics2DDrawFlags.Aabb |
      EPhysics2DDrawFlags.Pair |
      EPhysics2DDrawFlags.CenterOfMass |
      EPhysics2DDrawFlags.Joint |
      EPhysics2DDrawFlags.Shape
    // 注册单个碰撞体的回调函数
    let collider = this.node.getComponent(BoxCollider2D)
    console.log(23)
    console.log(collider)
    if (collider) {
      collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this)
      collider.on(Contact2DType.END_CONTACT, this.onEndContact, this)
      collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this)
      collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this)
    }
  }
  onBeginContact(
    selfCollider: Collider2D,
    otherCollider: Collider2D,
    contact: IPhysics2DContact | null
  ) {
    // 只在两个碰撞体开始接触时被调用一次
    console.log('onBeginContact', otherCollider)
  }
  onEndContact(
    selfCollider: Collider2D,
    otherCollider: Collider2D,
    contact: IPhysics2DContact | null
  ) {
    // 只在两个碰撞体结束接触时被调用一次
    console.log('onEndContact')
  }
  onPreSolve(
    selfCollider: Collider2D,
    otherCollider: Collider2D,
    contact: IPhysics2DContact | null
  ) {
    // 每次将要处理碰撞体接触逻辑时被调用
    console.log('onPreSolve')
  }
  onPostSolve(
    selfCollider: Collider2D,
    otherCollider: Collider2D,
    contact: IPhysics2DContact | null
  ) {
    // 每次处理完碰撞体接触逻辑时被调用
    console.log('onPostSolve')
  }
}
